package com.swift.violent.mainstay.config.data.source.config;

import cn.hutool.core.map.MapUtil;
import com.github.alenfive.rocketapi.datasource.DataSourceDialect;
import com.github.alenfive.rocketapi.datasource.DataSourceManager;
import com.github.alenfive.rocketapi.datasource.SqlDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;

/**
 * Rocket api 数据源配置
 *
 * @author xuhaifeng
 * @date 2021.1.28 09:59
 */
@Slf4j
@Configuration
public class RocketDataSourceConfig extends DataSourceManager {
    @Resource(name = "jdbcTemplateBase")
    private JdbcTemplate jdbcTemplateBase;

    /**
     * rocket-api 数据源初始化
     */
    @PostConstruct
    public void initRocketDataSource() {
        super.setDialectMap(
                MapUtil.builder(new HashMap<String, DataSourceDialect>(5))
                        .put("base", new SqlDataSource(jdbcTemplateBase, true))
                        .build()
        );
    }
}
